import Cookies from 'js-cookie'
import { getConfigKey } from '@/api/system/config'
import { getToken } from '@/utils/auth'

const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false
  },
  device: 'desktop',
  size: Cookies.get('size') || 'medium',
  extraProcess: {},
  componentLayout: 'tree',
  menuLayout: 'default',
  showEn: 'false',
  pageSize: '0',
  url: '',
  showIm: 'false'
}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 1)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SET_SIZE: (state, size) => {
    state.size = size
    Cookies.set('size', size)
  },
  TOGGLE_EXTRA_PROCESS: (state, data) => {
    state.extraProcess = {
      ...data
    }
  },
  SET_COMPONENTS_LAYOUT: (state, data) => {
    state.componentLayout = data
  },
  SET_MENU_LAYOUT: (state, data) => {
    state.menuLayout = data
  },
  SET_FILE_POTE_USER: (state, data) => {
    state.filePoteUser = data
  },
  SET_SHOW_EN: (state, data) => {
    state.showEn = data
  },
  SET_SHOW_IM: (state, data) => {
    state.showIm = data
  },
  SET_DEFAULT_PAGE_SIZE: (state, data) => {
    console.log(data)
    if (data) {
      state.pageSize = data
    } else {
      state.pageSize = '0'
    }
  },
  SET_CHAT_URL: (state) => {
    getConfigKey('im_show').then(res => {
      if (res.msg === 'true') {
        state.url = process.env.VUE_APP_IM_VUE + '?token=' + getToken()
      }
    })
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  setSize({ commit }, size) {
    commit('SET_SIZE', size)
  },
  toggleExtraProcess({ commit }, data) {
    commit('TOGGLE_EXTRA_PROCESS', data)
  },
  async getComponentsLayout({ commit }) {
    const res = await getConfigKey('user_components_type')
    commit('SET_COMPONENTS_LAYOUT', res.msg)
  },
  async getMenuLayout({ commit }) {
    var res = await getConfigKey('menu_layout')
    commit('SET_MENU_LAYOUT', res.msg)
  },
  async getFilePoteUser({ commit }) {
    var res = await getConfigKey('file_pote_user')
    commit('SET_FILE_POTE_USER', res.msg)
  },
  async getShowEn({ commit }) {
    var res = await getConfigKey('show_en')
    commit('SET_SHOW_EN', res.msg)
  },
  async getShowIm({ commit }) {
    var res = await getConfigKey('im_show')
    commit('SET_SHOW_IM', res.msg)
  },
  async getDefaultPageSize({ commit }) {
    var res = await getConfigKey('sys_default_page_size')
    commit('SET_DEFAULT_PAGE_SIZE', res.msg)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
